// app.js
App({
    onLaunch(options) {
        console.log(options);
        this.globalData.shareId = options.query.scene || 0
        console.log(this.globalData.shareId);
        var that = this;
        // wx.showModal({
        //     title: '分享人ID',
        //     content: `${that.globalData.shareId}`,
        //     showCancel: false,
        //     complete: (res) => {
        //         if (res.confirm) {
        //         }
        //     }
        // })

        this.checkLogin()
        this.onShareAppMessage()
    },
    onShareAppMessage() {
        var that = this;
        wx.onAppRoute(() => {
            const pages = getCurrentPages()
            const view = pages[pages.length - 1]
            console.log(pages);
            console.log(view);
            if (view.route == 'packageProduct/pages/productDetail/productDetail') return false

            if (!view) return false



            view.onShareAppMessage = () => {
                return {
                    title: '中肽悦享',
                    imageUrl: '/img/avatar.png',
                    path: '/pages/index/index?scene=' + that.globalData.userId
                }
            }
            view.onShareTimeline = () => {
                return {
                    title: '中肽悦享',
                    imageUrl: '/img/avatar.png',
                    query: `userId=${that.globalData.userId}`
                }
            }
        })
    },
    checkLogin() {
        var that = this, isLogin = wx.getStorageSync("isZhongHuiLogin");
        if (isLogin) {
            var user = JSON.parse(wx.getStorageSync('user'))
            console.log(user);
            that.globalData.user = user
            that.globalData.openId = user.openId
            that.globalData.userId = user.userId
            that.globalData.shopId = user.shopId || 0
        }
    },
    getInfo() {
        var that = this;
        return new Promise(function (resolve, reject) {
            console.log("登录");
            // 登录
            wx.login({
                success: res => {
                    console.log(res.code);
                    // 发送 res.code 到后台换取 openId, sessionKey, unionId
                    var url = that.globalData.url + "user/loginMini";
                    that.wxRequest(url, {
                        code: res.code,
                        parentId: that.globalData.shareId
                    }, res => {
                        console.log(res);
                        if (res.flag) {
                            resolve(res);
                        } else {
                            wx.showToast({
                                title: '信息验证失败，请重试',
                                icon: 'none'
                            })
                        }
                    }, err => { })
                }
            })
        })
    },

    wxRequest(url, data, succFuc, errFuc) {
        wx.request({
            url: url,
            data: data,
            method: 'POST',
            header: {
                'Content-type': 'application/x-www-form-urlencoded',
            },
            success: res => {
                succFuc(res.data);
            },
            fail: err => {
                errFuc(err);
            }
        })
    },

    wxRequestJSON(url, data, succFuc, errFuc) {
        console.log(data);
        wx.request({
            url: url,
            data: data,
            method: 'POST',
            header: {
                'Content-type': 'application/json;charset=utf-8',
            },
            success: res => {
                succFuc(res.data);
            },
            fail: err => {
                errFuc(err);
            }
        })
    },

    globalData: {
        // url: 'http://192.168.0.110:2040/',
        url: 'https://www.ccclkj.cn:2040/',
        path: "https://zbz-imgs.oss-cn-beijing.aliyuncs.com/",
        openId: '',
        userId: 0,
        shopId: 0,
        shareId: 0,// 我的上级id
        user: '',
        vipFlag: 0,// 0 不是会员  1 是会员
    }

})